import utils from "@/js/utils";
import {
    serverUrl
} from "@/js/settings";
import request from "@/js/request";
import moment from "./moment";

import "@/scss/base.scss";
import "@/scss/common.scss";
import "./index.scss";

$(function() {
    localStorage.removeItem("errorObject");
    //清除左右分数
    localStorage.removeItem("leftScore");
    localStorage.removeItem("rightScore");

    var count_down = 10;
    var object = new Array();
    var arr = generationTenNumber(3);
    console.log(arr);
    $.getJSON("../topic.json", function(data) {
        for (const i in arr) {
            var index = arr[i];
            object.push(data[index]);
        }
        var number = 0;
        addQuestion(number);
    });

    var errorObject = [];
    var err_index = 0;
    var number = 0;
    // addQuestion(number);
    var flag;
    var current_topic;

    var leftScore = 0;
    var rightScore = 0;
    localStorage.setItem("leftScore", leftScore);
    localStorage.setItem("rightScore", rightScore);

    function addQuestion(num) {
        fillScore();
        if (num / 2 == 0) {
            console.log("加分");
        } else {
            console.log("辣鸡，这都不对");
        }
        var size = object.length;
        if (num < size) {
            //积加分
            console.log("leftScore: " + localStorage.getItem("leftScore"));

            var html = "";
            var item = object[num];
            current_topic = item;
            html +=
                ` 
            <div class="prompt">知识点: ${item.prompt}</div>
            <div class="game_title">${item.title}</div>
            <div class="options"></div>
            <div class="optiona_line_1">
                <div class="optional_1 btn" id="optional_1">${item.optional_1}</div>
                <div class="optional_2 btn" id="optional_2">${item.optional_2}</div>
            </div>
            <div class="optiona_line_2">
                <div class="optional_3 btn" id="optional_3">${item.optional_3}</div>
                <div class="optional_4 btn" id="optional_4">${item.optional_4}</div>
            </div>
            `
            $(".game_content").html(html);
            $("#countDownTime").text(count_down);
            var countDownTime = parseInt(count_down - 1); //在这里设置每道题的答题时长
            countDown(countDownTime);
            flag = true;
        }
        $(".btn").click(function(_this) {
            var isAdd = addScore();
            console.log("is add: " + isAdd);
            if (flag) {
                console.log(_this.currentTarget);
                var answer = object[num].answer;
                var currentId = _this.currentTarget.attributes.id.value;
                console.log(answer + " " + currentId)
                var err = object[num];
                err.select = (currentId);
                // 右边虚拟用户控制是否加分
                if (isAdd) {
                    rightScore = parseInt(rightScore) + 10;
                    localStorage.setItem("rightScore", rightScore);
                }

                if (currentId != answer) {
                    $(_this.currentTarget).css("background", "#ff9292f7");
                    $("#" + answer).css("background", "#1fecae");
                } else {
                    $(_this.currentTarget).css("background", "#1fecae")
                    leftScore = parseInt(leftScore) + 10;
                    localStorage.setItem("leftScore", leftScore);
                }
                errorObject.push(err);
                localStorage.setItem("errorObject", JSON.stringify(errorObject));
                //清空时间
                clearInterval(timer);
                number++;
                interval(number);
                flag = false;
            }
        });
    }

    // var countDownTime = parseInt(9); //在这里设置每道题的答题时长
    var timer;

    function countDown(countDownTime) {
        timer = setInterval(function() {
            if (countDownTime >= 0) {
                showTime(countDownTime);
                countDownTime--;
            } else {
                clearInterval(timer);
                number++
                if (number < object.length) {
                    current_topic.select = 'optional_no';
                    errorObject.push(current_topic);
                    console.log(errorObject.length);
                    localStorage.setItem("errorObject", JSON.stringify(errorObject));
                    addQuestion(number);
                } else {
                    flag = false;
                    current_topic.select = 'optional_no';
                    errorObject.push(current_topic);
                    localStorage.setItem("errorObject", JSON.stringify(errorObject));
                    console.log("倒计时结束没了");
                    interval_jump();
                }
            }
        }, 1000);
    }
    // countDown(countDownTime);
    function showTime(countDownTime) { //这段是计算分和秒的具体数
        var minute = Math.floor(countDownTime / 60);
        var second = countDownTime - minute * 60;
        $("#countDownTime").text(second);
        $("#countDownTime").css("margin", "0 auto")
    }

    function interval(number) {
        if (number < object.length) {
            var interval = setInterval(function() {
                addQuestion(number);
                clearInterval(interval);
            }, Math.random() * 2000 + 1000);
        } else {
            console.log("点没了")
            interval_jump();
        }
    }

    // 延迟跳转
    function interval_jump() {
        var interval = setInterval(function() {
            window.location.href = "/settle.html";
            clearInterval(interval);
        }, Math.random() * 3000 + 500);
    }

    function diu_Randomize(b, e) {
        if (!b && b != 0 || !e) { return "?"; }
        return Math.floor((Math.random() * e) + b);
    }

    function generationTenNumber(n) {
        var i = 0;
        var arr = new Array;
        while (i < n) {
            var number = diu_Randomize(1, 19);
            arr[i] = number;
            i++;
        }
        return arr;
    }

    function addScore() {
        var number = diu_Randomize(1, 10);
        console.log("is add score for right: " + number)
        if (number % 2 == 0) {
            return true;
        }
        return false;
    }

    function fillScore() {
        var left_score = localStorage.getItem("leftScore");
        var rightScore = localStorage.getItem("rightScore");

        var left_html_score = "";
        left_html_score += `
        阿鬼鬼&nbsp&nbsp|&nbsp&nbsp<span>${left_score}</span>
        `
        $(".player_left_name").html(left_html_score);

        var right_html_score = "";
        right_html_score += `
        阿飒飒&nbsp&nbsp|&nbsp&nbsp<span>${rightScore}</span>
        `
        $(".player_right_name").html(right_html_score);
    }
})